<?php
/**
 * Created by JetBrains PhpStorm.
 * PointType: Kame
 * Date: 7/04/13
 * Time: 05:39 PM
 */

namespace Site\Model;

use Zend\Db\TableGateway\TableGateway;


class PointTypeTable {

    protected $tableGateway;

    public function __construct(TableGateway $gateway)
    {
        $this->tableGateway = $gateway;
    }

    public function fetchAll()
    {
        return $this->tableGateway->select();
    }

    /**
     * @param $tipopunto
     * @return \Site\Model\PointType
     */
    public function getPointTypeByID($tipopunto)
    {
        $tipopunto = (int)$tipopunto;
        $row = $this->tableGateway->select(array('tipo' => $tipopunto))->current();
        return $row;
    }

    public function savePointType(PointType $PointType)
    {
        $data = array(
            'descripcion' => $PointType->descripcion,
            'nombrecorto' => $PointType->nombrecorto,
            'etiquetas' => $PointType->etiquetas,
        );
        $tipo = (int)$PointType->tipo;
        if ($tipo == 0) {
            $this->tableGateway->insert($data);
            return true;
        } else {
            if($this->getPointTypeByID($tipo)){
                $this->tableGateway->update($data,array('tipo' => $tipo));
                return true;
            }
        }
        return false;
    }

}